<?php namespace Common\Model;
use Hdphp\Model\Model;
 
class sc_goods extends Model{
	
	//指定表
	protected $table="sc_goods";
	
//	自动验证
//	protected $validate=array(
//	    array('gnumber','regexp:/^\d{10}$/','商品货号必须是10位整数',3,3),
//	    array('gnumber','unique','商品货号已经存在',3,1)
//	);
	
	//自动完成
	protected $auto=array(
	    array('time','time','function',3,1),
        array('sc_admin_aid','getAdmin','method',3,1),
        array('pic','getPic','method',3,3),
        array('gnumber','makeGnumber','method',3,1)
	);
	

    //实例化引用模型
//	private $CategoryModel;
//	private $BrandModel;
    //商品详情表模型
    private $DetailModel;
	private $GoodsAttrModel;
	private $CategoryModel;
    public function __init(){
		$this->DetailModel=M('\Common\Model\sc_detail');
		$this->GoodsAttrModel=M('\Common\Model\sc_goods_attr');
		$this->CategoryModel=M('\Common\Model\sc_category');
	}

   

	//获取全部商品
	public function getGoods(){
		
		return $this->orderBy('gid','ASC')->get();
	}
	
	//获取某一分类下的商品(删除分类用)
	public function getCateGoods(){
		$cid=Q('get.cid');
		return $this->where('sc_category_cid',$cid)->field('gid')->find();	
	}
	
	
	//添加商品
	public function addGoods(){
		if(!$this->create()) return false;
		$gid = $this->add(); 
		//商品详情处理························
		if(!$this->DetailModel->addDetail($gid)){
			$this->error = $this->DetailModel->getError();
			return false;
		}
		
		//商品属性处理
		if(!$this->GoodsAttrModel->addGoodsAttr($gid)) return false;
		
		return true;
	}	
	
	//编辑获取一条旧商品数据
	public function getOneGoods(){
		$gid=Q('get.gid');
        return $this->where('gid',$gid)->find();

	}
    
	
		
	//修改保存商品数据
    public function saveOneGoods(){
		if(!$this->create()) return false;
		$this->save();
		//编辑商品详细
		if(!$this->DetailModel->saveOneDetail()) return false;
		
		//编辑商品属性信息
		if(!$this->GoodsAttrModel->saveOneGoodsAttr()) return false;
		
		//全部成功！
		return true;
    }
	
	
	//前台应用部分············································
	//通过cids获取gids
	public function getGids($cids){
        //将获取的分类集数组转换成字符串
		$cidStr=implode(",", $cids);
		//查询各分类下的商品id(gids)
		return $this->where("sc_category_cid IN ($cidStr)")->lists('gid');
    }
	
	
	//通过商品gids获取商品信息
	public function getGoodsListData($finalGids){
		$gids=implode(",", $finalGids);
		return $this->where("gid IN ($gids)")->field('gid','gname','shopprice','pic','click')->get();
		
	}
	
	
	//商品详细页，通过gid获取商品详情、商品属性、商品信息
	public function getDetailGoods(){
		$gid=Q('get.gid');
        $data=$this->join('sc_detail','sc_goods_gid','=',$gid)
                   ->where('gid',$gid)
                   ->get();
        
		foreach($data as $k=>$v){
			$data[$k]['small']=explode("|", $v['small']);
			$data[$k]['big']=explode("|", $v['big']);
		}
		
//		P($data);
		
		return $data;
    
	}
	
	
	//获取热门商品四个
	public function getHot(){
		return $this->where("click > 200")->limit(4)->field('gid','gname','pic','shopprice')->get();
	}
	
	
	//取得一个最新商品
	public function getOneNew(){
		
		return $this->orderBy('time','DESC')->limit(1)->find();
	}
	
	
	//取得一级分类下第一个分类下所有商品
	public function getNO1Goods($cids){
		return $this->where("sc_category_cid IN ({$cids})")->limit(7)->get();
	}
	
	
	//取得一级分类下第二个分类下所有商品
	public function getNO2Goods($cids){
		return $this->where("sc_category_cid IN ({$cids})")->limit(7)->get();
	}
	
	//取得一级分类下第三个分类下所有商品
	public function getNO3Goods($cids){
		return $this->where("sc_category_cid IN ({$cids})")->limit(7)->get();
	}
	
	//取得一级分类下第四个分类下所有商品
	public function getNO4Goods($cids){
		return $this->where("sc_category_cid IN ({$cids})")->limit(7)->get();
	}
	

	
	
	
	
	//自动完成商品货号
	public function makeGnumber(){
          return time();
	}


    

    
	
	//自动完成获取用户id
	public function getAdmin(){
		return $_SESSION['info']['uid'];
	}
	
	//自动完成修改图片
     public function getPic(){
     	$pic=Q('post.pic');
		$oldpic=Q('post.oldpic');
		if($pic){
			$pics=pathinfo($pic);
			$thumbImg=str_replace(".{$pics['extension']}", "_thumb.{$pics['extension']}", $pic);
			$img=Image::thumb($pic,$thumbImg,480,480,6);
			unlink($pic);
			if($oldpic){
				unlink($oldpic);
			}
			return $img;
		}     
		if($oldpic){
			return $oldpic;
		}
		return '';
     }
	 
	
	
}




?>